package 力扣日常刷题.木23一月.第12天312;

import java.util.PriorityQueue;

/**
 * @author 帅小伙
 * @date 2022/3/12
 * @description
 */
public class Demo48数组中第K个最大元素 {


    public int findKthLargest(int[] nums,int k) {
        int len = nums.length;
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
        for (int i = 0; i < k; i++) {
            priorityQueue.add(nums[i]);
        }
        for (int i = k; i < len; i++) {
            Integer peek = priorityQueue.peek();
            if(nums[i] > peek){
                priorityQueue.poll();
                priorityQueue.add(nums[i]);
            }
        }
        return priorityQueue.peek();
    }
}
